﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import net.wooga.pets.events.*;
    import __AS3__.vec.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import com.adobe.errors.*;

    public class GameBackgroundMediator extends Mediator implements ITicking {

		[Inject]
        public var gameBackground:GameBackground;
        private var _daytimes:Vector.<Array>;
        private var _hour:Number;
        private var _minutes:Number;
        private var _previousState:String;

        private static const MIDNIGHT_ONE_OCLOCK_LINKAGE:String = "BackgroundNight2";
        private static const DAWN_LINKAGE:String = "BackgroundDawnAfterNight";
        private static const SUNRISE_LINKAGE:String = "BackgroundSunsetBeforeNoon";
        private static const NOON_LINKAGE:String = "BackgroundNoon";
        private static const AFTERNOON_LINKAGE:String = "BackgroundSunsetAfterNoon";
        private static const EVENING_LINKAGE:String = "BackgroundDawnBeforeNight";

        override public function onRegister():void{
            this.initLookupTable();
            this.gameBackground.visible = true;
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.MINUTE));
            eventMap.mapListener(this.gameBackground.creditsButton, MouseEvent.CLICK, this.onCreditsClick);
            this.setDate();
        }
        private function onCreditsClick(_arg1:Event):void{
            dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.CREDITS_POPUP));
        }
        override public function preRemove():void{
            this._daytimes = new Vector.<Array>();
            super.preRemove();
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            this.setDate();
        }
        private function setDate():void{
            var _local1:Date = new Date();
            this._hour = _local1.hours;
            this._minutes = _local1.minutes;
            this.updateTimeState();
        }
        private function initLookupTable():void{
            this._daytimes = new Vector.<Array>();
            this._daytimes.push([MIDNIGHT_ONE_OCLOCK_LINKAGE, TimeConstants.MIDNIGHT_1_OCLOCK]);
            this._daytimes.push([DAWN_LINKAGE, TimeConstants.DAWN_5_OCLOCK]);
            this._daytimes.push([SUNRISE_LINKAGE, TimeConstants.SUNRISE_8_OCLOCK]);
            this._daytimes.push([NOON_LINKAGE, TimeConstants.AFTERNOON_13_OCLOCK]);
            this._daytimes.push([AFTERNOON_LINKAGE, TimeConstants.AFTERNOON_18_OCLOCK]);
            this._daytimes.push([EVENING_LINKAGE, TimeConstants.EVENING_21_OCLOCK]);
            this._daytimes.fixed = true;
        }
        private function updateTimeState():void{
            var _local2:String;
            var _local3:int;
            var _local4:int;
            var _local5:int;
            var _local1:String;
            if (this._hour < TimeConstants.MIDNIGHT_1_OCLOCK){
                _local2 = this._daytimes[(this._daytimes.length - 1)][0];
            } else {
                if ((((this._hour >= TimeConstants.MIDNIGHT_1_OCLOCK)) && ((this._hour < TimeConstants.EVENING_21_OCLOCK)))){
                    _local5 = 0;
                    while (_local5 < (this._daytimes.length - 1)) {
                        if ((((this._hour >= this._daytimes[_local5][1])) && ((this._hour < this._daytimes[(_local5 + 1)][1])))){
                            _local2 = this._daytimes[_local5][0];
                            break;
                        };
                        _local5++;
                    };
                } else {
                    if (this._hour >= TimeConstants.EVENING_21_OCLOCK){
                        _local2 = this._daytimes[(this._daytimes.length - 1)][0];
                    };
                };
            };
            if (_local2 == _local1){
                throw (new IllegalStateError("no match is found to set the background"));
            };
            this.setDaytime(_local2, _local3, _local4);
        }
        private function setDaytime(_arg1:String, _arg2:Number, _arg3:Number):void{
            if (_arg1 != this._previousState){
                this.gameBackground.updateLinkage(_arg1);
                this.gameBackground.setFrame(1);
                this.gameBackground.setBackgroundColor();
            };
            this._previousState = _arg1;
        }

    }
}//package net.wooga.pets.views 
